<?php
include "./common.php";
include "./dataconfig.php";
$total = intval($_CONFIG['guessallnum']);
$guesssimplenum = intval($_CONFIG['guesssimplenum']);
define("GUESS_SIMPLE_NUM",$guesssimplenum);
define("GUESS_HARD_NUM",$total-$guesssimplenum);

$today = date("Ymd",time());


/**
 * 自动抽题
 **/
$todayGuess = $db->get_one("SELECT * FROM ".tname("guesstoday")." WHERE date='$today'");
$ids = ""; //本日题ID集合 ID之间用,分隔
if(!$todayGuess){
	/**
	 * 第二天抽题时，将答对但未填写个人资料的题目恢复   规则限制：当前时间-答对时间》180分钟
	 **/
	$timestamp = time();
	$db->query("UPDATE ".tname("guess")." SET ifanswer=0 WHERE ifanswer=1 AND identity is null AND $timestamp-answertime>60*60*3");
	/**
	 * 20 简单
	 **/
	$query = $db->query("SELECT id FROM ".tname("guess")." WHERE ifsimple=1 AND ifcheck=1 AND ifanswer=0 ORDER BY RAND() LIMIT ".GUESS_SIMPLE_NUM);
	while($array = $db->fetch_array($query)){
		$simpleRs[] = $array['id'];
	}
	
	/**
	 * 30 困难
	 **/
	$query = $db->query("SELECT id FROM ".tname("guess")." WHERE ifsimple=0 AND ifcheck=1 AND ifanswer=0 ORDER BY RAND() LIMIT ".GUESS_HARD_NUM);
	while($array = $db->fetch_array($query)){
		$hardRs[] = $array['id'];
	}

	$result = array_merge($simpleRs,$hardRs);
	
	$ids = implode(",",$result);
	$db->query("INSERT INTO ".tname("guesstoday")." VALUES ('$today','$ids')");
}else{
	$ids  = $todayGuess['ids'];
	$result = $simpleRs = $hardRs = array();
	if($ids!=null && $ids!=""){
		$idRs = explode(",",$ids);
		$simpleClearNum=$hardClearNum=0;
		
		foreach($idRs as $array){
			$temp = $db->get_one("SELECT * FROM ".tname("guess")." WHERE id='$array'");
			if($temp==null || $temp=="" || $temp['ifcheck']!=1){
				if($temp['ifsimple']==1){
					$simpleClearNum++;
				}else{
					$hardClearNum++;
				}
			}else{
				$result[] = $array;
			}
		}
	}else{
			$simpleClearNum = GUESS_SIMPLE_NUM;
			$hardClearNum   = GUESS_HARD_NUM;
	}

	
	/**
	 *  简单 补充
	 **/
	$query = $db->query("SELECT id FROM ".tname("guess")." WHERE ifsimple=1 AND ifcheck=1 AND ifanswer=0 ORDER BY RAND() LIMIT $simpleClearNum ");
	while($array = $db->fetch_array($query)){
		$simpleRs[] = $array['id'];
	}

	/**
	 * 困难 补充
	 **/
	$query = $db->query("SELECT id FROM ".tname("guess")." WHERE ifsimple=0 AND ifcheck=1 AND ifanswer=0 ORDER BY RAND() LIMIT $hardClearNum ");
	while($array = $db->fetch_array($query)){
		$hardRs[] = $array['id'];
	}
	if($simpleClearNum>0 || $hardClearNum>0){
		$result = array_merge($simpleRs,$hardRs,$result);
		$ids = implode(",",$result);
		$db->query("UPDATE ".tname("guesstoday")." SET ids='$ids' WHERE date='$today'");
	}
}

if($ids){
	$where = " WHERE id IN ($ids) AND ifcheck=1  ";
	$order = " ORDER BY id ASC";
	$page = $_GET['page'];
	!$page && $page = 1;
	!$pageSize && $pageSize = 5;
	$limit = " LIMIT ".($page-1)*$pageSize.",$pageSize";

	$query = $db->query("SELECT title,id,ifanswer,answernum FROM ".tname("guess")." $where $order $limit ");
	while($array = $db->fetch_array($query)){
		$rs[] = $array;
	}


	$amount = $db->get_one("SELECT COUNT(id) as amount FROM ".tname("guess")." $where $order ");
	$pageNum = ceil($amount['amount']/$pageSize);

	if($page>=$pageNum) $lastPage = true;




	/**
	 * 中奖信息
	 **/
	$query = $db->query("SELECT identity FROM ".tname("guess")." $where AND ifanswer=1 AND identity!='' ORDER BY answertime DESC");
	while($array = $db->fetch_array($query)){
		$array['identity'] = hideIdentity($array['identity']);
		$award[] = $array;
	}

	$awardAmount = $db->get_one("SELECT COUNT(id) as amount FROM ".tname("guess")." $where AND ifanswer=1  AND identity!='' ");
	$awardNum = $awardAmount['amount'];
}

include template("tickets");
?>